import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["page", "size"];

/**
 * model - SQL执行界面
 * @date: 2018-9-27

 */
import { getResponse } from 'utils/utils';
import { queryUnifyIdpValue } from 'cpaas-front/lib/services/api';
import { fetchDataTable as _fetchDataTable, fetchTableField as _fetchTableField, fetchExecuteResult as _fetchExecuteResult, fetchSingleResult as _fetchSingleResult } from "../services/sqlExecuteService";
export default {
  namespace: 'sqlExecute',
  state: {
    // dataTableList: [], // 可访问的数据表
    // tableFieldList: [], // 表字段
    resultData: {},
    // 执行结果
    query: {},
    pagination: {},
    // 分页信息对象
    clickedValue: '',
    // 鼠标点击的树节点名称
    serverName: '',
    // 搜索框选中的服务名称
    executeSql: '',
    // 执行的SQL
    serverList: [],
    // 服务
    exportSql: '' // 导出结果的sql参数

  },
  effects: {
    // 获取服务
    fetchServer: /*#__PURE__*/_regeneratorRuntime.mark(function fetchServer(_ref, _ref2) {
      var payload, put, call, serverList;
      return _regeneratorRuntime.wrap(function fetchServer$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              put = _ref2.put, call = _ref2.call;
              _context.t0 = getResponse;
              _context.next = 5;
              return call(queryUnifyIdpValue, 'CPFM.DBIDE.SERVICE_NAME', {
                tenantId: payload.tenantId
              });

            case 5:
              _context.t1 = _context.sent;
              serverList = (0, _context.t0)(_context.t1);
              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  serverList: serverList
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchServer);
    }),
    // 查询可访问的数据表
    fetchDataTable: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDataTable(_ref3, _ref4) {
      var payload, call, _payload$page, page, _payload$size, size, query, response, result;

      return _regeneratorRuntime.wrap(function fetchDataTable$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _payload$page = payload.page, page = _payload$page === void 0 ? 0 : _payload$page, _payload$size = payload.size, size = _payload$size === void 0 ? 15 : _payload$size, query = _objectWithoutProperties(payload, _excluded);
              _context2.next = 5;
              return call(_fetchDataTable, _objectSpread({
                page: page,
                size: size
              }, query));

            case 5:
              response = _context2.sent;
              result = getResponse(response); // if (result) {
              //   yield put({
              //     type: 'updateState',
              //     payload: {
              //       query,
              //       dataTableList: result.content,
              //       pagination: createPagination(result),
              //     },
              //   });
              // }

              return _context2.abrupt("return", result);

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchDataTable);
    }),
    // 查询表对应的字段
    fetchTableField: /*#__PURE__*/_regeneratorRuntime.mark(function fetchTableField(_ref5, _ref6) {
      var payload, call, response, list;
      return _regeneratorRuntime.wrap(function fetchTableField$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_fetchTableField, payload);

            case 4:
              response = _context3.sent;
              list = getResponse(response);
              return _context3.abrupt("return", list);

            case 7:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchTableField);
    }),
    // 执行全部SQL
    fetchExecuteAllResult: /*#__PURE__*/_regeneratorRuntime.mark(function fetchExecuteAllResult(_ref7, _ref8) {
      var payload, call, put, response, listData;
      return _regeneratorRuntime.wrap(function fetchExecuteAllResult$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call, put = _ref8.put;
              _context4.next = 4;
              return call(_fetchExecuteResult, payload);

            case 4:
              response = _context4.sent;
              listData = getResponse(response);

              if (!listData) {
                _context4.next = 9;
                break;
              }

              _context4.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  resultData: listData
                }
              });

            case 9:
              return _context4.abrupt("return", listData);

            case 10:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchExecuteAllResult);
    }),
    // 执行选中的SQL
    fetchExecuteResult: /*#__PURE__*/_regeneratorRuntime.mark(function fetchExecuteResult(_ref9, _ref10) {
      var payload, call, put, response, listData;
      return _regeneratorRuntime.wrap(function fetchExecuteResult$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call, put = _ref10.put;
              _context5.next = 4;
              return call(_fetchExecuteResult, payload);

            case 4:
              response = _context5.sent;
              listData = getResponse(response);

              if (!listData) {
                _context5.next = 9;
                break;
              }

              _context5.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  resultData: listData
                }
              });

            case 9:
              return _context5.abrupt("return", listData);

            case 10:
            case "end":
              return _context5.stop();
          }
        }
      }, fetchExecuteResult);
    }),
    // 获取单条SQL翻页结果
    fetchSingleResult: /*#__PURE__*/_regeneratorRuntime.mark(function fetchSingleResult(_ref11, _ref12) {
      var payload, call, response, listData;
      return _regeneratorRuntime.wrap(function fetchSingleResult$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              _context6.next = 4;
              return call(_fetchSingleResult, payload);

            case 4:
              response = _context6.sent;
              listData = getResponse(response);
              return _context6.abrupt("return", listData);

            case 7:
            case "end":
              return _context6.stop();
          }
        }
      }, fetchSingleResult);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};